package com.youlu.campus.admin.order;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.admin.utils.IpUtils;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;

import com.youlu.campus.entity.VO.req.SingleOrderBatchQueryVO;
import com.youlu.campus.entity.order.SingleOrderBatch;
import com.youlu.campus.service.order.SingleOrderBatchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;

@Slf4j
@RestController
public class SingleOrderBatchController {
    @Autowired
    private SingleOrderBatchService singleOrderBatchService;

    @PostMapping("/single-order-batch/list")
    public RestResult list(@RequestBody @Valid SingleOrderBatchQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<SingleOrderBatch> datas = singleOrderBatchService.list(req);
        if (datas == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            datas = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(datas).build();
    }

    @PostMapping("/single-order-batch/create")
    public RestResult create(@RequestParam("name") String name,
                             @RequestParam("desc") String desc,
                             @RequestParam("activityId") String activityId,
                             @RequestParam("file") MultipartFile file) {
        String comId = AuthUtils.getComId();
        try {
            SingleOrderBatch req = new SingleOrderBatch();
            req.setName(name);
            req.setDesc(desc);
            req.setActivityId(activityId);
            boolean re = singleOrderBatchService.create(req, file.getBytes(), file.getOriginalFilename(), AuthUtils.getLoginName());
            if (re) {
                return RestResultBuilder.builder().success().build();
            }
            return RestResultBuilder.builder().failure().build();
        } catch (BusinessException b) {
            log.error(":>>> 新增发货批次错误业务异常:{}", b);
            throw b;
        } catch (Exception e) {
            log.error(":>>> 新增发货批次错误:{}", e);
            return RestResultBuilder.builder().failure().build();
        }
    }

    @PostMapping("/single-order-batch/update")
    public RestResult list(@RequestBody @Valid SingleOrderBatch req) {
        String comId = AuthUtils.getComId();
        boolean re = singleOrderBatchService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/single-order-batch/find-one")
    public RestResult findOne(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        SingleOrderBatch re = singleOrderBatchService.findOne(id);
        return RestResultBuilder.builder().success().data(re).build();
    }

    @GetMapping("/single-order-batch/delete")
    public RestResult delete(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = singleOrderBatchService.delete(id);
        return RestResultBuilder.builder().success().build();
    }

    @GetMapping("/single-order-batch/cancel")
    public RestResult cancel(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = singleOrderBatchService.cancel(id);
        return RestResultBuilder.builder().success().build();
    }

    @GetMapping("/single-order-batch/pre-do")
    public RestResult preDo(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = singleOrderBatchService.preDo(id);
        return RestResultBuilder.builder().success().build();
    }

    @GetMapping("/single-order-batch/do")
    public RestResult doing(@RequestParam("id") String id, HttpServletRequest request) {
        String comId = AuthUtils.getComId();
        boolean re = singleOrderBatchService.done(id, IpUtils.getIpAddr(request), AuthUtils.getLoginName());
        return RestResultBuilder.builder().success().build();
    }

    @PostMapping("/single-order-batch/manual-single")
    public RestResult manualSingle(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "desc",required = false) String desc,
            @RequestParam("activityId") String activityId,
            @RequestParam("file") MultipartFile file) {
        String comId = AuthUtils.getComId();
        try {
            boolean re = singleOrderBatchService.reverse(file.getBytes(), file.getOriginalFilename(), AuthUtils.getLoginName(), activityId);
            if (re) {
                return RestResultBuilder.builder().success().build();
            }
            return RestResultBuilder.builder().failure().build();
        } catch (BusinessException b) {
            log.error(":>>> 新增发货批次错误业务异常:{}", b);
            throw b;
        } catch (Exception e) {
            log.error(":>>> 新增发货批次错误:{}", e);
            return RestResultBuilder.builder().failure().build();
        }
    }
}
